package cz.data.common.feign;

import cz.data.common.support.YamlPropertySourceFactory;
import feign.Contract;
import feign.Retryer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.encoding.FeignClientEncodingProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;


@EnableFeignClients({"cz.data.domain.*.rpc", "cz.data.platform.*.rpc"})
@AutoConfigureOrder(0)
@PropertySource(value = "classpath:application-feign.yml", factory = YamlPropertySourceFactory.class, encoding = "UTF-8")
@AutoConfigureAfter(FeignAutoConfiguration.class)
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
public class DataFeignConfiguration {


    @Bean
    public RequestCompressionInterceptor requestCompressionInterceptor(FeignClientEncodingProperties properties) {
        return new RequestCompressionInterceptor(properties);
    }

//    @Bean
//    public Decoder feignDecoder() {
//        ObjectMapper objectMapper = new ObjectMapper();
//        SecurityModuleUtil.registerSimpleModule(objectMapper);
//        SecurityModuleUtil.registerJavaTimeModule(objectMapper);
//        SecurityModuleUtil.registerCustomizerModule(objectMapper);
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
//        HttpMessageConverters converters = new HttpMessageConverters(converter);
//        return new SpringDecoder(() -> converters);
//    }


    @Bean
    public Retryer retryer() {
        return Retryer.NEVER_RETRY;
    }

    @Bean
    public Contract feignContract() {
        return new DataContract();
    }
}
